package com.easy.controller;

import com.easy.bean.Record;
import com.easy.common.CommonResult;
import com.easy.service.IRecordService;
import com.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
public class RecordController {

    @Autowired
    IRecordService recordService;

    //通过ID获取
    @GetMapping("delivery/{id}")
    public Record getDeliveryByID(@PathVariable int id) {
        return recordService.getDeliveryByID(id);
    }
    @GetMapping("receipt/{id}")
    public Record getReceiptByID(@PathVariable int id) {
        return recordService.getReceiptByID(id);
    }

    //获取列表
    @GetMapping("delivery")
    @Transactional
    public CommonResult getDelivery(Record record, Page page) {
        List<Record> list =recordService.getDelivery(record,page);
        int count=recordService.getDeliveryCount(record);
        HashMap map=new HashMap();
        map.put("list",list);
        map.put("count",count);
        return CommonResult.success(map);
    }
    //获取列表
    @GetMapping("receipt")
    @Transactional
    public CommonResult getReceipt(Record record, Page page) {
        List<Record> list =recordService.getReceipt(record,page);
        int count=recordService.getReceiptCount(record);
        HashMap map=new HashMap();
        map.put("list",list);
        map.put("count",count);
        return CommonResult.success(map);
    }



    //删除
    @DeleteMapping("delivery/{id}")
    public CommonResult delDelivery(@PathVariable int id) {
        int count = recordService.delDelivery(id);
        if(count>0){
            return CommonResult.success(count);
        }else {
            return CommonResult.fail();
        }
    }
    @DeleteMapping("receipt/{id}")
    public CommonResult delReceipt(@PathVariable int id) {
        int count = recordService.delReceipt(id);
        if(count>0){
            return CommonResult.success(count);
        }else {
            return CommonResult.fail();
        }
    }


    //新增
    @PostMapping("delivery")
    @Transactional
    public CommonResult addDelivery(@RequestBody Record record){
        recordService.addDelivery(record);
        record = recordService.getDeliveryByID(record.getId());
        return CommonResult.success(record);
    }
    @PostMapping("receipt")
    @Transactional
    public CommonResult addReceipt(@RequestBody Record record){
        recordService.addReceipt(record);
        record = recordService.getReceiptByID(record.getId());
        return CommonResult.success(record);
    }


    //修改
    @PutMapping("delivery")
    @Transactional
    public CommonResult editDelivery(@RequestBody Record record) {
        recordService.editDelivery(record);
        record = recordService.getDeliveryByID(record.getId());
        return CommonResult.success(record);
    }
    @PutMapping("receipt")
    @Transactional
    public CommonResult editRecordJSON(@RequestBody Record record) {
        recordService.editReceipt(record);
        record = recordService.getReceiptByID(record.getId());
        return CommonResult.success(record);
    }

}
